package cn.edu.baiyunu.myapplication.data.repository

import android.graphics.Color
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import cn.edu.baiyunu.myapplication.data.db.entity.PhotoRecord
import cn.edu.baiyunu.myapplication.data.db.entity.UserPreference
import cn.edu.baiyunu.myapplication.data.model.PhotoSpec
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import java.util.Date
import kotlin.random.Random

/**
 * Mock偏好设置仓库，用于测试
 */
class MockPreferenceRepository : PreferenceRepository {
    override fun getUserPreference(): Flow<UserPreference> {
        return flowOf(
            UserPreference(
                id = "user_preference",
                defaultBackgroundColor = "蓝色",
                defaultSpecType = "一寸照",
                autoOptimizeEnabled = true,
                highQualityExport = true,
                lastUsedDirectory = null
            )
        )
    }
    
    override suspend fun updateUserPreference(userPreference: UserPreference) {
        // Mock实现，不执行实际操作
    }
    
    override fun getDefaultBackgroundColor(): Flow<String> {
        return flowOf("蓝色")
    }
    
    override suspend fun setDefaultBackgroundColor(color: String) {
        // Mock实现，不执行实际操作
    }
    
    override fun getDefaultSpecType(): Flow<String> {
        return flowOf("一寸照")
    }
    
    override suspend fun setDefaultSpecType(specType: String) {
        // Mock实现，不执行实际操作
    }
    
    override fun getAutoOptimizeEnabled(): Flow<Boolean> {
        return flowOf(true)
    }
    
    override suspend fun setAutoOptimizeEnabled(enabled: Boolean) {
        // Mock实现，不执行实际操作
    }
}

/**
 * Mock照片仓库，用于测试
 */
class MockPhotoRepository : PhotoRepository {
    
    // 模拟照片记录列表
    private val photos = MutableStateFlow<List<PhotoRecord>>(createMockPhotoRecords())
    
    override suspend fun savePhotoRecord(photoRecord: PhotoRecord): Long {
        val newId = photos.value.maxOfOrNull { it.id }?.plus(1) ?: 1
        val newPhoto = photoRecord.copy(id = newId)
        photos.value = photos.value + newPhoto
        return newId
    }
    
    override fun getAllPhotoRecords(): Flow<List<PhotoRecord>> {
        return photos
    }
    
    override fun getPhotoRecordById(id: Long): Flow<PhotoRecord?> {
        return photos.map { list -> list.find { it.id == id } }
    }
    
    override suspend fun getPhotoById(id: Long): PhotoRecord? {
        return photos.value.find { it.id == id }
    }
    
    override fun getPhotoRecordsBySpecType(specType: String): Flow<List<PhotoRecord>> {
        return photos.map { list -> list.filter { it.specType == specType } }
    }
    
    override suspend fun deletePhotoRecord(id: Long) {
        photos.value = photos.value.filter { it.id != id }
    }
    
    override suspend fun deleteAllPhotoRecords() {
        photos.value = emptyList()
    }
    
    override fun searchPhotoRecords(query: String): Flow<List<PhotoRecord>> {
        return photos.map { list -> 
            list.filter { 
                it.specType.contains(query, ignoreCase = true) || 
                it.backgroundColor.contains(query, ignoreCase = true)
            }
        }
    }
    
    override fun getRecentPhotoRecords(limit: Int): Flow<List<PhotoRecord>> {
        return photos.map { list -> 
            list.sortedByDescending { it.createdAt }.take(limit)
        }
    }
    
    /**
     * 创建模拟照片记录数据
     */
    private fun createMockPhotoRecords(): List<PhotoRecord> {
        val specTypes = listOf("一寸照", "二寸照", "小一寸", "小二寸", "护照照", "签证照")
        val backgroundColors = listOf("白色", "红色", "蓝色", "灰色")
        val currentTime = System.currentTimeMillis()
        val day = 24 * 60 * 60 * 1000L
        
        return List(15) { index ->
            PhotoRecord(
                id = (index + 1).toLong(),
                filePath = "/storage/emulated/0/Photos/photo_${index + 1}.jpg",
                thumbnailPath = "/storage/emulated/0/Photos/thumb_${index + 1}.jpg",
                specType = specTypes[Random.nextInt(specTypes.size)],
                backgroundColor = backgroundColors[Random.nextInt(backgroundColors.size)],
                createdAt = currentTime - (Random.nextInt(10) * day),
                width = when(index % 6) {
                    0 -> 25 // 一寸
                    1 -> 35 // 二寸
                    2 -> 22 // 小一寸
                    3 -> 35 // 小二寸
                    4 -> 33 // 护照
                    else -> 35 // 签证
                },
                height = when(index % 6) {
                    0 -> 35 // 一寸
                    1 -> 45 // 二寸
                    2 -> 32 // 小一寸
                    3 -> 49 // 小二寸
                    4 -> 48 // 护照
                    else -> 45 // 签证
                },
                isAiOptimized = Random.nextBoolean()
            )
        }
    }
}

/**
 * 用户偏好设置数据类
 */
data class UserPreference(
    val id: String = "user_preference",
    val defaultSpecType: String,
    val autoOptimizeEnabled: Boolean,
    val highQualityExport: Boolean,
    val lastUsedDirectory: String?
) 